package com.dhcc.DRGsHosp.facade.impl;

import com.dhcc.DRGsHosp.application.FileUploadApplication;
import com.dhcc.DRGsHosp.core.domain.*;
import com.dhcc.DRGsHosp.facade.FileUploadFacade;
import com.dhcc.DRGsHosp.facade.XPathParseFacade;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.StringUtils;
import org.dom4j.Element;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.inject.Named;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 
 * @title		:XmlStructureFacadeImpl.java
 * @description	:
 * @author		:mazhaoyan
 * @date		:2017年4月18日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
@Named("drgsHospFacadeImplMzy180128")
public class XPathParseFacadeImpl implements XPathParseFacade{
	private static final Logger logger = LoggerFactory.getLogger(XPathParseFacadeImpl.class);
	@Autowired
	private FileUploadApplication fileUploadApplication;
	@Autowired
	private FileUploadFacade fileUploadFacade;
    
    @SuppressWarnings("unchecked")
	public  Map<String,Object> parseXmlNode(Element element,Map<String,Object> initMap,Map<String,Object> xPathMap){
    	Map<String,Object> caseMap = new HashMap<String,Object>();//病例信息map
    	
    	//map 不要每次都去查询 传进来
    	Map<String,String> dataTypeMap = (Map<String, String>) initMap.get("dataTypeMap");
		// 基本信息
		MrBaseInfo mrBaseInfo = new MrBaseInfo();
		// 住院信息
		MrIpInfo mrIpInfo = new MrIpInfo();
		// 附页基本信息
		MrAppendPage mrAppendInfo = new MrAppendPage();
		//诊断list
		List<MrDiagnose> diagList = new ArrayList<MrDiagnose>();
		//手术list
    	List<MrOperation> operList = new ArrayList<MrOperation>();
    	//费用list
    	List<MrExpense> expList = new ArrayList<MrExpense>();
    	
    	// 基本信息
    	List<Map<String,Object>> baseXPathMap = (List<Map<String, Object>>) xPathMap.get("base");
    	// 住院信息
		List<Map<String,Object>> ipXPathMap = (List<Map<String, Object>>) xPathMap.get("ip");
		// 附页信息
		List<Map<String,Object>> appendXPathMap = (List<Map<String, Object>>) xPathMap.get("append");
		// 费用信息
		List<Map<String,Object>> expXPathMap = (List<Map<String, Object>>) xPathMap.get("exp");
		// 诊断信息
		Map<String,List<Map<String,Object>>> diagMap = (Map<String, List<Map<String, Object>>>) xPathMap.get("diag");
		// 手术信息
		Map<String,List<Map<String,Object>>> operMap = (Map<String, List<Map<String, Object>>>) xPathMap.get("oper");

		String pMainIndex = UUID.randomUUID().toString();// 基本信息表主键;
		String mrNo = "";// 病案号
		String mrOutDate = "";// 出院日期
		String mrInHospCount = "";// 住院次数
		String xmlPathInfo = "";//记录当前解析的xmlPath
		String fieldCode = "";//字段代码
		String errInfo = "";//错误信息
    	String fieldErr = "";//字段错误
		try {
			Map<String,Object> fieldMap = new HashMap<String,Object>();
			fieldMap.put("PMAININDEX","PmainIndex");
			//处理基本信息
	    	for(int i = 0;i<baseXPathMap.size();i++){
	    		xmlPathInfo = baseXPathMap.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		fieldCode = baseXPathMap.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null){//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值

				//处理年龄带“岁”的
				if (value != null && value.matches("^(\\d+\\.\\d+|\\d+|\\.\\d+)岁$")) {
                    value = value.replaceAll("^(\\d+\\.\\d+|\\d+|\\.\\d+)岁$", "$1");
				}

    			fieldErr = DrgsIndex.setElementValueFileUpload(mrBaseInfo,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
    			if(StringUtils.hasLength(fieldErr)){
    				errInfo += "MR_BASEINFO "+ fieldCode + " " + fieldErr+"\n";
    			}
	    	}
	    	fileUploadFacade.baseDictMap(mrBaseInfo,(Map<String,Object>)initMap.get("dictData"));//字典匹配
			
			//处理住院信息
	    	for(int i = 0;i<ipXPathMap.size();i++){
	    		xmlPathInfo = ipXPathMap.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		fieldCode = ipXPathMap.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null){//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值
	    		fieldErr = DrgsIndex.setElementValueFileUpload(mrIpInfo,fieldCode,value,dataTypeMap.get(fieldCode),null);
    			if(StringUtils.hasLength(fieldErr)){
    				errInfo += "MR_IPINFO "+ fieldCode + " " + fieldErr+"\n";
    			}
	    	}
	    	
	    	fileUploadFacade.ipDictMap(mrIpInfo,(Map<String,Object>)initMap.get("dictData"));//字典匹配
	    	synchronized (initMap) {
	    		fileUploadFacade.parseDepDoc(mrBaseInfo,mrIpInfo,initMap);
			}
			//处理附页信息
	    	for(int i = 0;i<appendXPathMap.size();i++){
	    		xmlPathInfo = appendXPathMap.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		fieldCode = appendXPathMap.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null){//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值
	    		fieldErr = DrgsIndex.setElementValueFileUpload(mrAppendInfo,fieldCode,value,dataTypeMap.get(fieldCode),null);
    			if(StringUtils.hasLength(fieldErr)){
    				errInfo += "MR_APPENDINFO "+ fieldCode + " " + fieldErr+"\n";
    			}
	    	}
			//设置主键  优先级 ->系统外部传入 ->指定具体字段->病案号+出院日期+住院次数 
			if(initMap.get("PMAININDEX")!=null){
				pMainIndex = initMap.get("PMAININDEX").toString();
			}else if(StringUtils.hasLength(mrBaseInfo.getPmainIndex())){
				pMainIndex = mrBaseInfo.getPmainIndex();
			}else {
				if(mrBaseInfo.getP00050000()!=null){
					mrNo = mrBaseInfo.getP00050000();
				}
				if(mrIpInfo.getP00290000()!=null){
					mrOutDate = new SimpleDateFormat("yyyy-MM-dd").format(mrIpInfo.getP00290000());
				}
				if(mrBaseInfo.getP00040000()!=null){
					mrInHospCount = mrBaseInfo.getP00040000()+"";
				}
				pMainIndex = mrNo + "-" + mrOutDate + "-" + mrInHospCount;
			}
			mrBaseInfo.setPmainIndex(pMainIndex);
			mrIpInfo.setPmainIndex(pMainIndex);
			mrAppendInfo.setPmainIndex(pMainIndex);
			
			//计算新生儿天数(月/天)
			if(mrBaseInfo.getP00080000()!=null && mrIpInfo.getP00250000()!=null){
				//计算天数
				int days = (int) ((mrIpInfo.getP00250000().getTime() - mrBaseInfo.getP00080000().getTime()) / (1000*3600*24));
				if(days < 365){
					if(days < 1){
						mrBaseInfo.setP00090002(1);
					} else if (days < 30){
						mrBaseInfo.setP00090002(days);
					} else {
						mrBaseInfo.setP00090001(days/30); //取余
						mrBaseInfo.setP00090002(days%30); //取模
					}
					mrBaseInfo.setP00090000(0);
				}
			}
			
			//处理费用信息
			int expIndex = 0;
	    	for(int i = 0;i<expXPathMap.size();i++){
	    		xmlPathInfo = expXPathMap.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		String code = expXPathMap.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null){//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值
	    		if(value.matches("^(\\-|\\+)?(\\d+\\.\\d+|\\d+|\\.\\d+)$")){
		    		double tmpC = Double.parseDouble(value);
		    		if((Map<String,CostSub>)initMap.get("mapCostSub")==null) {
		    			errInfo+="请配置费用分类信息";
		    		}else {
						MrExpense mr_expense = new MrExpense();
						mr_expense.setId(UUID.randomUUID().toString());// ID
						mr_expense.setPe0101(code);// 费用名称
						mr_expense.setPe0200(tmpC);// 费用值
						mr_expense.setPeIndex(expIndex++);//序号码
						mr_expense.setPmainIndex(pMainIndex);// 病例外键
						expList.add(mr_expense);
		    		}
    			}
	    	}
			
			//处理诊断信息
			xmlPathInfo = "处理诊断";
			fieldErr = parseDiags(element,pMainIndex,diagList,diagMap,initMap);//处理诊断
			if(StringUtils.hasLength(fieldErr)){
				errInfo += fieldErr;
			}
			
			//处理手术信息
			xmlPathInfo = "处理手术";
			fieldErr = parseOpers(element,pMainIndex,mrIpInfo,operList,operMap,initMap);//处理手术
			if(StringUtils.hasLength(fieldErr)){
				errInfo += fieldErr;
			}
			//返回数据
			caseMap.put("pMainIndex",pMainIndex);
			caseMap.put("base",mrBaseInfo);
			caseMap.put("ip",mrIpInfo);
			caseMap.put("append",mrAppendInfo);
			caseMap.put("diag",diagList);
			caseMap.put("oper",operList);
			caseMap.put("exp",expList);
		} catch (Exception e) {
			e.printStackTrace();
			errInfo += xmlPathInfo+ " " +e.getMessage();
		}finally {
			if(StringUtils.hasLength(errInfo)){
				caseMap = new HashMap<>();//病例有问题 数据置空
				//写入log日志
				logger.error(errInfo);
			}
		}
		System.err.println("生产的数据为："+caseMap);
		return caseMap;
	}

    @SuppressWarnings("unchecked")
	public String parseDiags(Element element, String pMainIndex,List<MrDiagnose> diagList,Map<String, List<Map<String, Object>>> diagMap,Map<String,Object> initMap) throws Exception{
    	Map<String,Object> fieldMap = new HashMap<String,Object>();
    	Map<String,String> dataTypeMap = (Map<String, String>) initMap.get("dataTypeMap");
    	fieldMap.put("PDTYPE","pdType");
    	String xmlPathInfo = "";
    	String fieldCode = "";
    	String errInfo = "";//错误信息
    	String fieldErr = "";//字段错误
    	int diagIndex = 0;
    	//处理诊断信息
		if(diagMap.containsKey("A")){//所有节点都重复  主要诊断和其他诊断 要判断 诊断类型是否有映射
			List<Map<String, Object>> diagPathList = diagMap.get("A");
			String repeat = diagPathList.get(0).get("TITLE_NAME").toString();
			repeat = org.apache.commons.lang.StringUtils.substringBefore(repeat,"θ");
			//获取一共有多少个节点
			Node nodeDiag = element.selectSingleNode(repeat);
			if(nodeDiag!=null){
				Node nodeDiagParent = nodeDiag.selectSingleNode("..");
				List<Node> nodeChild = nodeDiagParent.selectNodes(nodeDiag.getName());
				for(int i=0;i<nodeChild.size();i++){
					MrDiagnose mrDiag = new MrDiagnose();
					for(int j=0;j<diagPathList.size();j++){
						xmlPathInfo = diagPathList.get(j).get("TITLE_NAME").toString();//当前解析的路径
			    		fieldCode = diagPathList.get(j).get("FIELD_CODE").toString();//字段代码
			    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]");
			    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
			    		if(node==null){//对应路径的节点不存在
							continue;//当前节点不存在 跳过
						}
			    		String value = node.getText();//获取节点值
			    		fieldErr = DrgsIndex.setElementValueFileUpload(mrDiag,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
		    			if(StringUtils.hasLength(fieldErr)){
		    				errInfo += "MR_DIAGNOSE "+i+" "+ fieldCode + " " + fieldErr+"\n";
		    			}
					}
					//判断一下
					if(!DrgsIndex.isAllFieldNull(mrDiag)){//诊断编码不为空 保存
						mrDiag.setId(UUID.randomUUID().toString());
						mrDiag.setPmainIndex(pMainIndex);
						if(diagIndex==0&&mrDiag.getPdType()==null){
							mrDiag.setPdType("M");
						}else if(mrDiag.getPdType()==null){
							mrDiag.setPdType("O");
						}
						mrDiag.setPdIndex(diagIndex++);
						diagList.add(mrDiag);
					}
				}
			}
		}else if(diagMap.containsKey("O")){//其他诊断 重复
			//主诊断
			List<Map<String, Object>> mainDiagPathList = diagMap.get("M");
			MrDiagnose mrDiag = new MrDiagnose();
			for(int i=0;i<mainDiagPathList.size();i++){
				xmlPathInfo = mainDiagPathList.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		fieldCode = mainDiagPathList.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null){//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值
	    		fieldErr = DrgsIndex.setElementValueFileUpload(mrDiag,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
    			if(StringUtils.hasLength(fieldErr)){
    				errInfo += "MAIN MR_DIAGNOSE "+ fieldCode + " " + fieldErr+"\n";
    			}
			}
			//判断一下
			if(!DrgsIndex.isAllFieldNull(mrDiag)){//诊断编码不为空 保存
				mrDiag.setId(UUID.randomUUID().toString());
				mrDiag.setPmainIndex(pMainIndex);
				if(mrDiag.getPdType()==null){
					mrDiag.setPdType("M");
				}
				mrDiag.setPdIndex(diagIndex++);
				diagList.add(mrDiag);
			}
			//其他诊断
			List<Map<String, Object>> otherDiagPathList = diagMap.get("O");
			String repeat = otherDiagPathList.get(0).get("TITLE_NAME").toString();
			repeat = org.apache.commons.lang.StringUtils.substringBefore(repeat,"θ");
			//获取一共有多少个节点
			Node nodeDiag = element.selectSingleNode(repeat);
			if(nodeDiag!=null){
				Node nodeDiagParent = nodeDiag.selectSingleNode("..");
				List<Node> nodeChild = nodeDiagParent.selectNodes(nodeDiag.getName());
				for(int i=0;i<nodeChild.size();i++){
					MrDiagnose otherMrDiag = new MrDiagnose();
					for(int j=0;j<otherDiagPathList.size();j++){
						xmlPathInfo = otherDiagPathList.get(j).get("TITLE_NAME").toString();//当前解析的路径
			    		fieldCode = otherDiagPathList.get(j).get("FIELD_CODE").toString();//字段代码
			    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]");
			    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
			    		if(node==null){//对应路径的节点不存在
							continue;//当前节点不存在 跳过
						}
			    		String value = node.getText();//获取节点值
			    		fieldErr = DrgsIndex.setElementValueFileUpload(otherMrDiag,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
		    			if(StringUtils.hasLength(fieldErr)){
		    				errInfo += "other "+i+" MR_DIAGNOSE "+ fieldCode + " " + fieldErr+"\n";
		    			}
					}
					//判断一下
					if(!DrgsIndex.isAllFieldNull(otherMrDiag)){//诊断编码不为空 保存
						otherMrDiag.setId(UUID.randomUUID().toString());
						otherMrDiag.setPmainIndex(pMainIndex);
						if(otherMrDiag.getPdType()==null){
							otherMrDiag.setPdType("O");
						}
						otherMrDiag.setPdIndex(diagIndex++);
						diagList.add(otherMrDiag);
					}
				}
			}
		}else {//各个诊断都不重复
			for (String key : diagMap.keySet()) {
				List<Map<String, Object>> diagPathList = diagMap.get(key);
				// 诊断信息
				MrDiagnose mrDiag = new MrDiagnose();
				for(int i=0;i<diagPathList.size();i++){
					xmlPathInfo = diagPathList.get(i).get("TITLE_NAME").toString();//当前解析的路径
		    		fieldCode = diagPathList.get(i).get("FIELD_CODE").toString();//字段代码
		    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
		    		if(node==null){//对应路径的节点不存在
						continue;//当前节点不存在 跳过
					}
		    		String value = node.getText();//获取节点值
		    		fieldErr = DrgsIndex.setElementValueFileUpload(mrDiag,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
	    			if(StringUtils.hasLength(fieldErr)){
	    				errInfo += "MR_DIAGNOSE "+key+" "+ fieldCode + " " + fieldErr+"\n";
	    			}
				}
				//判断一下
				if(!DrgsIndex.isAllFieldNull(mrDiag)){//诊断编码不为空 保存
					mrDiag.setId(UUID.randomUUID().toString());
					mrDiag.setPmainIndex(pMainIndex);
					if("M".equals(key)&&mrDiag.getPdType()==null){
						mrDiag.setPdType("M");
					}else if(mrDiag.getPdType()==null){
						mrDiag.setPdType("O");
					}
					mrDiag.setPdIndex(diagIndex++);
					diagList.add(mrDiag);
				}
			}
		}
    	return errInfo;
    }
    //针对 北京卫统4 解析手术方法 
	/*@SuppressWarnings("unchecked")
	public String parseOpers(Element element,String pMainIndex,MrIpInfo mrIpInfo,List<MrOperation> operList,Map<String, List<Map<String, Object>>> operMap,Map<String,Object> initMap) throws Exception
    {
    	Map<String,Object> fieldMap = new HashMap<String,Object>();
		Map<String,Object> dictData = (Map<String, Object>) initMap.get("dictData");
		Map<String,String> dataTypeMap = (Map<String, String>) initMap.get("dataTypeMap");
    	fieldMap.put("POTYPE","poType");
    	String xmlPathInfo = "";
    	String fieldCode = "";
    	String errInfo = "";//错误信息
    	String fieldErr = "";//字段错误
    	int operIndex = 0;
    	List<Map<String, Object>> operPathList = operMap.get("A");
    	List<Map<String, Object>> operPathL1 = new ArrayList<Map<String, Object>>();
    	List<Map<String, Object>> operPathL2 = new ArrayList<Map<String, Object>>();
    	for(int i = 0;i<operPathList.size();i++)
		{
			String path = operPathList.get(i).get("TITLE_NAME").toString();
			if(path.contains("Ω"))
			{
				operPathL2.add(operPathList.get(i));
			}
			else if(path.contains("θ"))
			{
				operPathL1.add(operPathList.get(i));
			}
		}
    	String repeat1 = operPathL1.get(0).get("TITLE_NAME").toString();
    	repeat1 = org.apache.commons.lang.StringUtils.substringBefore(repeat1,"θ");
    	//获取一共有多少个节点
		Node nodeOper1 = element.selectSingleNode(repeat1);
		if(nodeOper1!=null)
		{
			Node nodeOper1Parent = nodeOper1.selectSingleNode("..");
			List<Node> nodeChild1 = nodeOper1Parent.selectNodes(nodeOper1.getName());
			for(int i=0;i<nodeChild1.size();i++)
			{
				MrOperation mrOper1 = new MrOperation();
				for(int j=0;j<operPathL1.size();j++)
				{
					xmlPathInfo = operPathL1.get(j).get("TITLE_NAME").toString();//当前解析的路径
		    		fieldCode = operPathL1.get(j).get("FIELD_CODE").toString();//字段代码
		    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]");
		    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
		    		if(node==null)
					{//对应路径的节点不存在
						continue;//当前节点不存在 跳过
					}
		    		String value = node.getText();//获取节点值
		    		fieldErr = DrgsIndex.setElementValueFileUpload(mrOper1,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
	    			if(StringUtils.hasLength(fieldErr)){
	    				errInfo += "MR_OPERATION "+i+" "+ fieldCode + " " + fieldErr+"\n";
	    			}
				}
				String repeat2 = operPathL2.get(0).get("TITLE_NAME").toString();
		    	repeat2 = org.apache.commons.lang.StringUtils.substringBefore(repeat2,"Ω");
		    	repeat2 = repeat2.replace("θ","["+(i+1)+"]");
		    	//获取一共有多少个节点
				Node nodeOper2 = element.selectSingleNode(repeat2);
				if(nodeOper2!=null)
				{
					Node nodeOper2Parent = nodeOper2.selectSingleNode("..");
					List<Node> nodeChild2 = nodeOper2Parent.selectNodes(nodeOper2.getName());
					for(int k=0;k<nodeChild2.size();k++)
					{
						MrOperation mrOper2 = new MrOperation();
						mrOper2 = mrOper1.copy();
						for(int l=0;l<operPathL2.size();l++)
						{
							xmlPathInfo = operPathL2.get(l).get("TITLE_NAME").toString();//当前解析的路径
				    		fieldCode = operPathL2.get(l).get("FIELD_CODE").toString();//字段代码
				    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]").replace("Ω","["+(k+1)+"]");
				    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
				    		if(node==null)
							{//对应路径的节点不存在
								continue;//当前节点不存在 跳过
							}
				    		String value = node.getText();//获取节点值
				    		fieldErr = DrgsIndex.setElementValueFileUpload(mrOper2,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
			    			if(StringUtils.hasLength(fieldErr)){
			    				errInfo += "MR_OPERATION "+i+" "+ fieldCode + " " + fieldErr+"\n";
			    			}
						}
						
						//判断一下
						if(!DrgsIndex.isAllFieldNull(mrOper2))
						{//不为空 保存
							mrOper2.setId(UUID.randomUUID().toString());
							mrOper2.setPmainIndex(pMainIndex);
							if("1".equals(mrOper2.getPoType()))
							{
								mrOper2.setPoType("M");
							}
							else if("2".equals(mrOper2.getPoType())){
								mrOper2.setPoType("O");
							}
							if("2".equals(mrOper2.getPo1400())){
								mrOper2.setPo1400("0");//2是操作
							}
							else
							{//默认手术
								mrOper2.setPo1400("1");
							}
							synchronized (initMap) {
								fileUploadFacade.parseOperDoc(initMap,mrOper2);
							}
							fileUploadFacade.operDictMap(mrOper2,dictData);
							mrOper2.setPoIndex(operIndex++);
							operList.add(mrOper2);
						}
					}
				}
			}
		}
		return errInfo;
    }*/
    //常规解析手术的方法
    @SuppressWarnings("unchecked")
	public String parseOpers(Element element,String pMainIndex,MrIpInfo mrIpInfo,List<MrOperation> operList,Map<String, List<Map<String, Object>>> operMap,Map<String,Object> initMap) throws Exception
    {
    	Map<String,Object> fieldMap = new HashMap<String,Object>();
    	Map<String,Object> dictData = (Map<String, Object>) initMap.get("dictData");
    	Map<String,String> dataTypeMap = (Map<String, String>) initMap.get("dataTypeMap");
    	fieldMap.put("POTYPE","poType");
    	String xmlPathInfo = "";
    	String fieldCode = "";
    	String errInfo = "";//错误信息
    	String fieldErr = "";//字段错误
    	int operIndex = 0;
		//处理手术信息
		if(operMap.containsKey("A"))
		{//所有节点都重复  主要手术和其他手术 要判断 手术类型是否有映射
			List<Map<String, Object>> operPathList = operMap.get("A");
			String repeat = operPathList.get(0).get("TITLE_NAME").toString();
			repeat = org.apache.commons.lang.StringUtils.substringBefore(repeat,"θ");
			//获取一共有多少个节点
			Node nodeOper = element.selectSingleNode(repeat);
			if(nodeOper!=null)
			{
				Node nodeOperParent = nodeOper.selectSingleNode("..");
				List<Node> nodeChild = nodeOperParent.selectNodes(nodeOper.getName());
				for(int i=0;i<nodeChild.size();i++)
				{
					MrOperation mrOper = new MrOperation();
					for(int j=0;j<operPathList.size();j++)
					{
						xmlPathInfo = operPathList.get(j).get("TITLE_NAME").toString();//当前解析的路径
			    		fieldCode = operPathList.get(j).get("FIELD_CODE").toString();//字段代码
			    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]");
			    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
			    		if(node==null)
						{//对应路径的节点不存在
							continue;//当前节点不存在 跳过
						}
			    		String value = node.getText();//获取节点值
			    		fieldErr = DrgsIndex.setElementValueFileUpload(mrOper,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
		    			if(StringUtils.hasLength(fieldErr)){
		    				errInfo += "main MR_OPERATION "+i+" "+fieldCode + " " + fieldErr+"\n";
		    			}
					}
					//判断一下
					if(!DrgsIndex.isAllFieldNull(mrOper))
					{//不为空 保存
						mrOper.setId(UUID.randomUUID().toString());
						mrOper.setPmainIndex(pMainIndex);
						if(operIndex==0&&mrOper.getPoType()==null)
						{
							mrOper.setPoType("M");
						}
						else if(mrOper.getPoType()==null){
							mrOper.setPoType("O");
						}
						synchronized (initMap) {
							fileUploadFacade.parseOperDoc(initMap,mrOper);
						}
						fileUploadFacade.operDictMap(mrOper,dictData);
						mrOper.setPoIndex(operIndex++);
						operList.add(mrOper);
					}
				}
			}
		}
		else if(operMap.containsKey("O"))
		{//其他手术 重复
			//主诊断
			List<Map<String, Object>> mainOperPathList = operMap.get("M");
			MrOperation mrOper = new MrOperation();
			for(int i=0;i<mainOperPathList.size();i++)
			{
				xmlPathInfo = mainOperPathList.get(i).get("TITLE_NAME").toString();//当前解析的路径
	    		fieldCode = mainOperPathList.get(i).get("FIELD_CODE").toString();//字段代码
	    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
	    		if(node==null)
				{//对应路径的节点不存在
					continue;//当前节点不存在 跳过
				}
	    		String value = node.getText();//获取节点值
	    		fieldErr = DrgsIndex.setElementValueFileUpload(mrOper,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
    			if(StringUtils.hasLength(fieldErr)){
    				errInfo += "main MR_OPERATION "+ fieldCode + " " + fieldErr+"\n";
    			}
			}
			//判断一下
			if(!DrgsIndex.isAllFieldNull(mrOper))
			{//不为空 保存
				mrOper.setId(UUID.randomUUID().toString());
				mrOper.setPmainIndex(pMainIndex);
				if(mrOper.getPoType()==null)
				{
					mrOper.setPoType("M");
				}
				synchronized (initMap) {
					fileUploadFacade.parseOperDoc(initMap,mrOper);
				}
				fileUploadFacade.operDictMap(mrOper,dictData);
				mrOper.setPoIndex(operIndex++);
				operList.add(mrOper);
			}
			//其他诊断
			List<Map<String, Object>> otherOperPathList = operMap.get("O");
			String repeat = otherOperPathList.get(0).get("TITLE_NAME").toString();
			repeat = org.apache.commons.lang.StringUtils.substringBefore(repeat,"θ");
			//获取一共有多少个节点
			Node nodeOper = element.selectSingleNode(repeat);
			if(nodeOper!=null)
			{
				Node nodeOperParent = nodeOper.selectSingleNode("..");
				List<Node> nodeChild = nodeOperParent.selectNodes(nodeOper.getName());
				for(int i=0;i<nodeChild.size();i++)
				{
					MrOperation otherMrOper = new MrOperation();
					for(int j=0;j<otherOperPathList.size();j++)
					{
						xmlPathInfo = otherOperPathList.get(j).get("TITLE_NAME").toString();//当前解析的路径
			    		fieldCode = otherOperPathList.get(j).get("FIELD_CODE").toString();//字段代码
			    		xmlPathInfo = xmlPathInfo.replace("θ","["+(i+1)+"]");
			    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
			    		if(node==null)
						{//对应路径的节点不存在
							continue;//当前节点不存在 跳过
						}
			    		String value = node.getText();//获取节点值
			    		fieldErr = DrgsIndex.setElementValueFileUpload(otherMrOper,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
		    			if(StringUtils.hasLength(fieldErr)){
		    				errInfo += "other "+i+" MR_OPERATION "+ fieldCode + " " + fieldErr+"\n";
		    			}
					}
					//判断一下
					if(!DrgsIndex.isAllFieldNull(otherMrOper))
					{//诊断编码不为空 保存
						otherMrOper.setId(UUID.randomUUID().toString());
						otherMrOper.setPmainIndex(pMainIndex);
						if(otherMrOper.getPoType()==null){
							otherMrOper.setPoType("O");
						}
						synchronized (initMap) {
							fileUploadFacade.parseOperDoc(initMap,otherMrOper);
						}
						fileUploadFacade.operDictMap(otherMrOper,dictData);
						otherMrOper.setPoIndex(operIndex++);
						operList.add(otherMrOper);
					}
				}
			}
		}
		else {//各个诊断都不重复
			for (String key : operMap.keySet()) {
				List<Map<String, Object>> operPathList = operMap.get(key);
				// 诊断信息
				MrOperation mrOper = new MrOperation();
				for(int i=0;i<operPathList.size();i++)
				{
					xmlPathInfo = operPathList.get(i).get("TITLE_NAME").toString();//当前解析的路径
		    		fieldCode = operPathList.get(i).get("FIELD_CODE").toString();//字段代码
		    		Node node = element.selectSingleNode(xmlPathInfo);//获取节点
		    		if(node==null)
					{//对应路径的节点不存在
						continue;//当前节点不存在 跳过
					}
		    		String value = node.getText();//获取节点值
		    		fieldErr = DrgsIndex.setElementValueFileUpload(mrOper,fieldCode,value,dataTypeMap.get(fieldCode),fieldMap);
	    			if(StringUtils.hasLength(fieldErr)){
	    				errInfo += "MR_OPERATION "+key+" "+ fieldCode + " " + fieldErr+"\n";
	    			}
				}
				//判断一下
				if(!DrgsIndex.isAllFieldNull(mrOper))
				{//诊断编码不为空 保存
					mrOper.setId(UUID.randomUUID().toString());
					mrOper.setPmainIndex(pMainIndex);
					if("M".equals(key)&&mrOper.getPoType()==null)
					{
						mrOper.setPoType("M");
					}
					else if(mrOper.getPoType()==null){
						mrOper.setPoType("O");
					}
					synchronized (initMap) {
						fileUploadFacade.parseOperDoc(initMap,mrOper);
					}
					fileUploadFacade.operDictMap(mrOper,dictData);
					mrOper.setPoIndex(operIndex++);
					operList.add(mrOper);
				}
			}
		}
		return errInfo;
    }
}
